package cn.com.cifi.mars.schedled;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import cn.com.cifi.mars.bean.bo.SysLogBO;
import cn.com.cifi.mars.constant.Constants;
import cn.com.cifi.mars.constant.IsSuccessEnum;
import cn.com.cifi.mars.constant.LogEventEnum;
import cn.com.cifi.mars.constant.LogSrcEnum;
import cn.com.cifi.mars.constant.LogTypeEnum;
import cn.com.cifi.mars.entity.SysJob;
import cn.com.cifi.mars.service.CommonService;
import cn.com.cifi.mars.service.DmCityReturnCoefficientService;
import cn.com.cifi.mars.service.DmProjectReturnCoefficientReportService;
import cn.com.cifi.mars.service.ProjectReturnCoefficientService;
import cn.com.cifi.mars.service.ReturnCoefficientDetailService;
import cn.com.cifi.mars.service.SysJobService;
import cn.com.cifi.mars.service.SysLogService;
import cn.com.cifi.mars.util.LocalDateTimeUtils;
import cn.com.cifi.mars.util.WebUtils;
import lombok.extern.slf4j.Slf4j;


/**
 * 回款系数版本及回款系数报表定时任务
 *
 * @Author: ztz
 * @Date: 2020/11/17 10:27
 */
@Slf4j
@Component
public class ReturnCoefficientTasks {
    @Autowired
    private ReturnCoefficientDetailService returnCoefficientDetailService;
    @Autowired
    private ProjectReturnCoefficientService projectReturnCoefficientService;
    @Autowired
    private SysLogService sysLogService;
    @Autowired
    private SysJobService sysJobService;
    @Autowired
    private DmCityReturnCoefficientService dmCityReturnCoefficientService;
    @Autowired
    private DmProjectReturnCoefficientReportService dmProjectReturnCoefficientReportService;
    @Autowired
    private CommonService commonService;

    /**
     * 计算实际版回款系数
     */
    @Scheduled(cron = "0 0 6 1 * ?")
    public void calculateRealityCoefficient() {
    	boolean appointIP = commonService.isAppointIP();
        if (!appointIP){
            return;
        }
        SysJob job = sysJobService.selectJobByCode(Constants.ZERO_TWENTY_SEVEN);
        LocalDateTime start = null;
        LocalDateTime end = null;
        if (null != job) {
            Integer jobId = job.getId();
            int count = sysJobService.lockJobById(jobId);
            LocalDateTime lastUpdateTime = job.getUpdateTime();
            boolean flag = LocalDateTimeUtils.isValidTime(lastUpdateTime, Constants.THREE);
            if (count == 1 && !flag) {
                try {
                    start = LocalDateTime.now();
                    //生成实际版回款系数
                    returnCoefficientDetailService.createAllPractical();
                    end = LocalDateTime.now();
                    saveLog("定时任务", "生成实际版回款系数", "生成实际版回款系数");
                } catch (Exception e) {
                    log.error("context:" + e);
                    saveLog("定时任务", "生成实际版回款系数", e.toString());
                } finally {
                    sysJobService.resetJob(job, start, end);
                }
            } else {
                sysJobService.resetStatus(job);
            }
        }
    }


    /**
     * <p>
     * 拉取投资版回款系数
     */
    @Scheduled(cron = "0 0 6 1 * ?")
    public void calculateInvestCoefficient() {
        SysJob job = sysJobService.selectJobByCode(Constants.ZERO_TWENTY_EIGHT);
        LocalDateTime start = null;
        LocalDateTime end = null;
        if (null != job) {
            Integer jobId = job.getId();
            int count = sysJobService.lockJobById(jobId);
            LocalDateTime lastUpdateTime = job.getUpdateTime();
            boolean flag = LocalDateTimeUtils.isValidTime(lastUpdateTime, Constants.THREE);
            if (count == 1 && !flag) {
                try {
                    start = LocalDateTime.now();
                    //同步投资板回款系数
                    projectReturnCoefficientService.syncInvestmentCoefficient(null);
                    saveLog("定时任务", "拉取投资版回款系数", "拉取投资版回款系数");
                    end = LocalDateTime.now();
                } catch (Exception e) {
                    log.error("context:" + e);
                    saveLog("定时任务", "拉取投资版回款系数", e.toString());
                } finally {
                    sysJobService.resetJob(job, start, end);
                }
            } else {
                sysJobService.resetStatus(job);
            }

        }

    }

    /**
     * <p>
     * 生成城市实际版回款系数
     */
    @Scheduled(cron = "0 0 6 * * ?")
    public void calculateCityRealityCoefficient() {
    	boolean appointIP = commonService.isAppointIP();
        if (!appointIP){
            return;
        }
        SysJob job = sysJobService.selectJobByCode(Constants.ZERO_TWENTY_NINE);
        LocalDateTime start = null;
        LocalDateTime end = null;
        if (null != job) {
            Integer jobId = job.getId();
            int count = sysJobService.lockJobById(jobId);
            LocalDateTime lastUpdateTime = job.getUpdateTime();
            boolean flag = LocalDateTimeUtils.isValidTime(lastUpdateTime, Constants.THREE);
            if (count == 1 && !flag) {
                try {
                    start = LocalDateTime.now();
                    //获取当前时间的yearMonth
                    String yearMonth = LocalDateTimeUtils.getLocalMonth(LocalDateTime.now());
                    dmCityReturnCoefficientService.refreshDmCityReturnCoefficient(yearMonth);
                    saveLog("定时任务", "生成城市实际版回款系数", "生成城市实际版回款系数");
                    end = LocalDateTime.now();
                } catch (Exception e) {
                    log.error("context:" + e);
                    saveLog("定时任务", "生成城市实际版回款系数", e.toString());
                } finally {
                    sysJobService.resetJob(job, start, end);
                }
            } else {
                sysJobService.resetStatus(job);
            }
        }

    }

    /**
     * 刷新项目回款系数分析表
     */
    @Scheduled(cron = "0 0 9 * * ?")
    public void calculateProjectReturnCoefficient() {
    	boolean appointIP = commonService.isAppointIP();
		if (!appointIP){
			return;
		}
        SysJob job = sysJobService.selectJobByCode(Constants.ZERO_THIRTY);
        LocalDateTime start = null;
        LocalDateTime end = null;
        if (null != job) {
            Integer jobId = job.getId();
            int count = sysJobService.lockJobById(jobId);
            LocalDateTime lastUpdateTime = job.getUpdateTime();
            boolean flag = LocalDateTimeUtils.isValidTime(lastUpdateTime, Constants.THREE);
            if (count == 1 && !flag) {
                try {
                    start = LocalDateTime.now();
                    //获取当前时间的yearMonth
                    String yearMonth = LocalDateTimeUtils.getLocalMonth(LocalDateTime.now());
                    dmProjectReturnCoefficientReportService.refreshdmProjectReturnCoefficient(null, yearMonth);
                    saveLog("定时任务", "刷新项目回款系数分析表", "刷新项目回款系数分析表");
                    end = LocalDateTime.now();
                } catch (Exception e) {
                    log.error("context:" + e);
                    saveLog("定时任务", "刷新项目回款系数分析表", e.toString());
                } finally {
                    sysJobService.resetJob(job, start, end);
                }
            } else {
                sysJobService.resetStatus(job);
            }
        }

    }

    //日志
    private boolean saveLog(String module, String title, String content) {
        SysLogBO bo = new SysLogBO();
        bo.setEvent(LogEventEnum.BROWSE);
        bo.setSource(LogSrcEnum.PC);
        String hostAddr = WebUtils.getLocalIP();
        bo.setIp(hostAddr);
        bo.setModule(module);
        bo.setTitle(title);
        bo.setContent(content);
        bo.setType(LogTypeEnum.ZERO.getKey());
        bo.setIsSuccess(IsSuccessEnum.SUCCESS.getKey());
        return sysLogService.add(bo, Constants.SYS);
    }


}
